package com.wondersgroup.insurance.dnuilearn.recyclerview.swipe;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.View;

/**
 * Created by Administrator on 2017/6/28.
 */

public class MyItemDecorder extends RecyclerView.ItemDecoration {
    int orientation = 0;
    final int height = 10;


    Paint paint;
    public MyItemDecorder() {
        super();
        paint = new Paint();
        paint.setColor(Color.RED);
    }

    @Override
    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
        super.onDraw(c, parent, state);
    }

    @Override
    public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
        if (parent.getAdapter() == null) {
            return;
        }
        if (orientation == OrientationHelper.VERTICAL) {
//                outRect.bottom = mHeight;
            drawVertical(c, parent);
        } else {
//                outRect.right = mHeight;
        }
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();

        if (layoutManager instanceof StaggeredGridLayoutManager) {
            orientation = ((StaggeredGridLayoutManager) layoutManager).getOrientation();
        } else if (layoutManager instanceof GridLayoutManager) {
            orientation = ((GridLayoutManager) layoutManager).getOrientation();
        } else if (layoutManager instanceof LinearLayoutManager) {
            orientation = ((LinearLayoutManager) layoutManager).getOrientation();
        }
        if (orientation == OrientationHelper.VERTICAL) {
//                outRect.bottom = mHeight;
            outRect.set(0, 0, 0, height);
        } else {
//                outRect.right = mHeight;
            outRect.set(0, 0, height, 0);
        }


//        super.getItemOffsets(outRect, view, parent, state);

    }

    public void drawVertical(Canvas c, RecyclerView parent) {
//            int childCount = parent.getAdapter().getItemCount();//  显示的个数是 所以不能用这个
        int childCount = parent.getChildCount();
        int left ,top,right,bootom;
        left = parent.getLeft()+parent.getPaddingLeft();
        right = parent.getRight()- parent.getPaddingRight();

        for (int i=0;i<childCount;i++){
            View child = parent.getChildAt(i);
            RecyclerView.LayoutParams params  = (RecyclerView.LayoutParams) child.getLayoutParams();


            top = params.bottomMargin+child.getBottom();

            bootom = top +height;
            c.drawRect(left,top,right,bootom,paint);
        }
    }
}